package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.Conn;
import com.lc.afbx.conn.PostApiLogin;
import com.lc.afbx.util.MEditTextListener;
import com.lc.afbx.util.Validator;
import com.zcx.helper.app.AppException;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/5/31.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private WebView login_wv;
    private Button Login_bt;
    private EditText Login_usename_et;
    private EditText login_password_et;
    private TextView login_lose_password_tv;
    private TextView Login_register_tv;

    private PostApiLogin postApiLogin = new PostApiLogin(new AsyCallBack<PostApiLogin.Info>() {

        @Override
        public void onSuccess(String toast, int type, PostApiLogin.Info info) throws Exception {

            ToastUtils.show(context, "登录成功");

            BaseApplication.BasePreferences.saveIsLogin(true);

            BaseApplication.BasePreferences.saveIsSetPay(Boolean.parseBoolean(info.data.paycode));//是否设置支付
            BaseApplication.BasePreferences.saveUserId(info.data.id);                           //登录返回id
            BaseApplication.BasePreferences.saveUserName(info.data.username);                    //用户名字

            if (!TextUtils.isEmpty(info.data.rec_code)) {

                BaseApplication.BasePreferences.saveCode(info.data.rec_code);                    //客户经理编码  上级的

            }


            try {
                //回调
                ((PersonalCenterActivity.geRenZhongXinCallBack) getAppCallBack(PersonalCenterActivity.class)).refresh();

            } catch (AppException e) {
                e.printStackTrace();
            }

            finish();    //关闭方法

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login_wv = ((WebView) findViewById(R.id.login_wv));
        //
        Login_usename_et = ((EditText) findViewById(R.id.Login_usename_et));
        login_password_et = ((EditText) findViewById(R.id.login_password_et));
        Login_bt = ((Button) findViewById(R.id.Login_bt));
        // 忘记密码，快速注册
        login_lose_password_tv = ((TextView) findViewById(R.id.login_lose_password_tv));
        Login_register_tv = ((TextView) findViewById(R.id.Login_register_tv));


        login_wv.setHorizontalScrollBarEnabled(false);//水平不显示
        login_wv.setVerticalScrollBarEnabled(false); //垂直不显示
        login_wv.loadUrl(Conn.SERVICE + "Api/Set/logattention");


        Login_bt.setOnClickListener(this);
        login_lose_password_tv.setOnClickListener(this);
        Login_register_tv.setOnClickListener(this);
        Login_usename_et.addTextChangedListener(new MEditTextListener(context, 11, Login_usename_et));
        login_password_et.addTextChangedListener(new MEditTextListener(context, 20, login_password_et));
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.Login_bt:

                String userNameStr = Login_usename_et.getText().toString().trim();
                String passwordStr = login_password_et.getText().toString().trim();

                if (TextUtils.isEmpty(userNameStr)) {
                    UtilToast.show(this, "账号不能为空");
                    return;
                } else if (!Validator.isMobile(userNameStr)) {
                    UtilToast.show(this, "手机号格式错误");
                    return;
                } else if (TextUtils.isEmpty(passwordStr)) {
                    UtilToast.show(this, "密码不能为空");
                    return;
                }
                postApiLogin.username = userNameStr;
                postApiLogin.password = passwordStr;

                postApiLogin.execute(this);
                return;

            case R.id.Login_register_tv:

                startActivity(new Intent(this, RegistActivity.class));

                break;

            case R.id.login_lose_password_tv:

                startActivity(new Intent(this, LosePasswordActivity.class));

                break;


        }


    }

}
